package com.bjpowernode.shop.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/*
    Feign请求拦截器，负责将发送请求前，添加请求头信息，传递令牌
        微服务A(包含请求头) -> 微服务B ...
        RabbitMQ的消费者(没有请求头) -> 微服务A -> 微服务B ...
        第三方回调(微信支付的回调,没有token令牌) -> 微服务A -> 微服务B ...
 */
@Configuration
public class RequestServerConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 获取请求对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        if (!ObjectUtils.isEmpty(attributes)) {
            HttpServletRequest request = attributes.getRequest();

            if (!ObjectUtils.isEmpty(request)) {
                
                // 支付宝回调方法中更新数据库的请求没有token，为其添加token
                String url = requestTemplate.url();
                if (url.contains("/p/myOrder/updateIsPay")) {
                    requestTemplate.header("Authorization", "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJhbGwiXSwiZXhwIjozODI3NDkwMjA2LCJqdGkiOiIxNjBhOWQzZS1kYzlkLTRiNzMtOGIzNi0zZDgxNjYzOWM5OTAiLCJjbGllbnRfaWQiOiJwb3dlciJ9.HEJyU_u1ji8cRdUMeUC0GDhpkm_KiL19ny20WQyAHVABO9K_SUUqOqLYdLGLffugD5hbjKh3kT1YV-XF2ptvhzMgKjc5d2sY4CweIiEUbGrp4tejPJiY4L_F7_u3xva58BzCAKDlmjqiNnNorowWBAdTlGAqDbUDYgqSsgHjsdnJLWDL2njn3AUnPoST8bX9CUac0mscIzb3JuCgyz5wM4HyG8FO4Ub78bkqWoMSLsyofk5WvVElA6IVcj5s4ivncZVPv0I9RE8BFpwcbmEC0jQ4ntxwZ-5851d94fRIeG5kgFMwEutbS1FmJTZ8Hf2rqqH3PerH6XNb4-4WKYhrXA");
                } else {
                    // 获取请求头信息
                    String header = request.getHeader("Authorization");
                    // 如果请求头中有token
                    if (StringUtils.hasText(header)) {
                        // 传递请求头信息
                        requestTemplate.header("Authorization", header);
                    }
                }
            } else {
                // 执行到此处的是 没有请求头或请求头中没有token的请求
                // 微信第三方的回调或消息队列的监听，都是前台项目，前台项目是客户端授权(所有用户公用一个token)
                // 为这些特殊请求设置全局token
                requestTemplate.header("Authorization", "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJhbGwiXSwiZXhwIjozODI3NDkwMjA2LCJqdGkiOiIxNjBhOWQzZS1kYzlkLTRiNzMtOGIzNi0zZDgxNjYzOWM5OTAiLCJjbGllbnRfaWQiOiJwb3dlciJ9.HEJyU_u1ji8cRdUMeUC0GDhpkm_KiL19ny20WQyAHVABO9K_SUUqOqLYdLGLffugD5hbjKh3kT1YV-XF2ptvhzMgKjc5d2sY4CweIiEUbGrp4tejPJiY4L_F7_u3xva58BzCAKDlmjqiNnNorowWBAdTlGAqDbUDYgqSsgHjsdnJLWDL2njn3AUnPoST8bX9CUac0mscIzb3JuCgyz5wM4HyG8FO4Ub78bkqWoMSLsyofk5WvVElA6IVcj5s4ivncZVPv0I9RE8BFpwcbmEC0jQ4ntxwZ-5851d94fRIeG5kgFMwEutbS1FmJTZ8Hf2rqqH3PerH6XNb4-4WKYhrXA");
            }
        } else {
            requestTemplate.header("Authorization", "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJhbGwiXSwiZXhwIjozODI3NDkwMjA2LCJqdGkiOiIxNjBhOWQzZS1kYzlkLTRiNzMtOGIzNi0zZDgxNjYzOWM5OTAiLCJjbGllbnRfaWQiOiJwb3dlciJ9.HEJyU_u1ji8cRdUMeUC0GDhpkm_KiL19ny20WQyAHVABO9K_SUUqOqLYdLGLffugD5hbjKh3kT1YV-XF2ptvhzMgKjc5d2sY4CweIiEUbGrp4tejPJiY4L_F7_u3xva58BzCAKDlmjqiNnNorowWBAdTlGAqDbUDYgqSsgHjsdnJLWDL2njn3AUnPoST8bX9CUac0mscIzb3JuCgyz5wM4HyG8FO4Ub78bkqWoMSLsyofk5WvVElA6IVcj5s4ivncZVPv0I9RE8BFpwcbmEC0jQ4ntxwZ-5851d94fRIeG5kgFMwEutbS1FmJTZ8Hf2rqqH3PerH6XNb4-4WKYhrXA");
        }
    }
}
